<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_cost".
 *
 * @property string $id
 * @property string $type
 * @property integer $personid
 * @property string $person
 * @property string $price
 * @property string $description
 * @property integer $createtime
 */
class BusinessCost extends \yii\db\ActiveRecord
{


    const STATUS_NORMAL = 0;
    const STATUS_CONFIRM = 1;
    public static $statues = [
        self::STATUS_NORMAL => '未确认支出',
        self::STATUS_CONFIRM => '确认支出'
    ];


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '成本',      //用于修改了什么的显示
        'mainfield' => 'id',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];




    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_cost';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'price', 'description', 'createtime','pigid'], 'required'],
            [['personid','pigid','status','typeid'], 'integer'],
            [['price'], 'number'],
            [['type'], 'string', 'max' => 20],
            ['createtime','safe'],
            [['person', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'type' => '类型',
            'typeid' => '成本类型',
            'personid' => '业务员',
            'person' => '业务员',
            'price' => '成本金额',
            'description' => '描述',
            'createtime' => '创建时间',
            'pigid' => '针对的仔猪',
            'status' => '状态'

        ];
    }




    public function beforeSave($insert)
    {
        if(empty($this->personid)){
            $this->personid = 0;
            $this->person = Yii::$app->user->identity->username;
        }else{
            $person = BusinessPerson::findOne($this->personid);
            $this->person = $person?$person->name:'';
        }

        $this->createtime = strtotime($this->createtime);
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }



    /**
     * @return \yii\db\ActiveQuery
     * 获取仔猪信息
     */
    public function getPig()
    {
        return $this->hasOne(BusinessPig::className(),['id'=>'pigid']);
    }

    public function getPersona()
    {
        return $this->hasOne(BusinessPerson::className(),['id'=>'personid']);
    }


}
